/**
 * Created by bgy on 2016/12/6.
 * 我的水果
 */
import React from 'react';
import {Link} from 'react-router';

class GooodsItemComponent extends React.Component{
  selectGoods(){
    this.props.selectGoods(this.props.item.goodsId);
  }
  handleStatus(){
    this.props.handleStatus(this.props.item.status,this.props.item.vendorGoodsId);
  }
  render() {
    return (
      <ul className='fruit-item'>
        <li>
          <h3>{this.props.item.goodsName}</h3>
          <div className="fruit-info rel">
            <span className="leftAB">更新时间：{this.props.item.modifyTime}</span>
            <span className="rightAB" onClick={this.selectGoods.bind(this)}> 查看水果详情></span>
          </div>
          <div className="fruit-status rel">
            <span className="leftAB"> <img width="20" src={this.props.item.imgSrc}/>{this.props.item.transStatus}</span>
            <span className="rightAB" onClick={this.handleStatus.bind(this)}> {this.props.item.transStatusHandle}></span>
          </div>
        </li>
      </ul>
    )
  }
}

class GoodsListCompent extends React.Component {
  constructor() {
    super();
    this.selectGoods=this.selectGoods.bind(this);
    this.handleStatus=this.handleStatus.bind(this);
    this.state={
      myFruits: [
        //测试数据
        //{
        //  goodsId: "1",
        //  goodsName: "水晶红富士",
        //  goodsCode: "234234",
        //  modifyTime: '2016/11/11',
        //  status: 'A',    //商品状态
        //  vendorGoodsId:'1'
        //}, {
        //  goodsId: "2",
        //  goodsName: '红玫瑰葡萄',
        //  goodsCode: '234236',
        //  modifyTime: '2016/11/13',
        //  status: 'B',
        //  vendorGoodsId:'2'
        //}, {
        //  goodsId: "3",
        //  goodsName: '新疆红提',
        //  goodsCode: '234230',
        //  modifyTime: '2016/11/13',
        //  status: 'C',
        //  vendorGoodsId:'3'
        //}, {
        //  goodsId: "4",
        //  goodsName: '东北打大枣',
        //  goodsCode: '234239',
        //  modifyTime: '2016/11/13',
        //  status: 'D',
        //  vendorGoodsId:'4'
        //}
      ]
    }
  }
  componentDidMount() {
    $.ajax(
      {
        'url':'http://192.168.2.17:8080/trade/goods/queryGoodsList',
        contentType: 'application/json',
        type: 'post',
        dataType: 'JSON',
        data: JSON.stringify({'vendorId': '10001'}),
        success: function (data) {
          console.info(data);
          if (data.errorCode == 0) {
            this.transData(data.data);
          } else {

          }
        }.bind(this),
        error: function () {
          //window.console.log(data.errorCode);
          return null;
        }
      }
    );
  }
  transData(transMyFruits){
    transMyFruits.forEach(function (value) {
      switch (value.status) {
        case 'A':
          value.transStatus = "质检通过";
          value.transStatusHandle = "查看质检报告";
          value.imgSrc = "images/myFruit/icon-pass@2x.png";
          break;
        case 'B':
          value.transStatus = "尚未申请质检";
          value.transStatusHandle = "申请质检";
          break;
        case 'C':
          value.transStatus = "您的水果未通过百果园的水果质量检测";
          value.imgSrc = "images/myFruit/icon-failure@2x.png";
          value.transStatusHandle = "查看质检报告";
          break;
        case 'D':
          value.transStatus = "已提交质检申请";
          value.imgSrc = "images/myFruit/icon-audit@2x.png";
          value.transStatusHandle = "查看申请";
          break;
        case 'E':
          value.transStatus = "已取消";
          value.imgSrc = "../../images/myFruit/icon-audit@2x.png";
          value.transStatusHandle = "查看申请";
          break;
      }
    });
    this.setState({
      myFruits: transMyFruits
    })
  }
  selectGoods(goodsId){
    this.props.showDetail(goodsId);
  }
  handleStatus(status,vendorGoodsId){
    this.props.showQuality(status,vendorGoodsId);
  }
render(){
  if (this.state.myFruits.length <1) {
    return (
      <section className='item-list tc'>
        <div className='top20'></div>
      </section>
    );
  }
  var options = this.state.options;
  var selectGoods=this.selectGoods;
  var handleStatus=this.handleStatus;
  var onScrollStart=this.onScrollStart;
  return (
    <section className='item-list'>
      {
        this.state.myFruits.map(function (item) {
          return (
            <GooodsItemComponent item={item} key={item.goodsId} selectGoods={selectGoods} handleStatus={handleStatus}/>
          );
        })
      }
    </section>
  );
}


}
export default GoodsListCompent;
